﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL.Entity;
using DAL.Persistencia;

namespace NewTierra.Pages
{
    public partial class AddDescCategoria : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopularGridDesc();
            }
        }

        #region Popular Dados

        protected void PopularGridDesc()
        {
            try
            {
                TarifCategoriaDAL td = new TarifCategoriaDAL();

                GridDesc.DataSource = td.ListarTodos(Convert.ToInt32(Session["Sid"]));
                GridDesc.DataBind();

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void PaginarDados(object sender, GridViewPageEventArgs e)
        {
            GridDesc.PageIndex = e.NewPageIndex;
            PopularGridDesc();
        }

        protected void GridDesc_SelectedIndexChanged(object sender, EventArgs e)
        {
            int myid = Convert.ToInt32(GridDesc.SelectedRow.Cells[0].Text);

            //int myid = Convert.ToInt32(e.CommandArgument);

            CategDescricaoDAL cdd = new CategDescricaoDAL();


            CategDescricao cd = cdd.ObterPorSupCateg(Convert.ToInt32(Session["Sid"]), myid);

            if (cd != null)
            {
                txtRemarkDescCateg.Text = cd.CategDesc;
                ViewState["IdCategDescr"] = cd.CategDesc_id;
            }
            else
            {
                txtRemarkDescCateg.Text = "";
                ViewState["IdCategDescr"] = "";
            }

            ViewState["IdCateg"] = myid;

            lblMsgCadDesc.Text = "";
        }

        #endregion

        #region CRUD Descrição

        protected void btnSalvarDesc_Click(object sender, EventArgs e)
        {
            try
            {

                if (ViewState["IdCategDescr"] == null)
                {
                    lblMsgCadDesc.Text = "Favor selecione uma categoria.";
                    return;
                }

                CategDescricaoDAL cdd = new CategDescricaoDAL();
                CategDescricao cd = new CategDescricao();

                CategDescricao cd2 = cdd.ObterPorSupCateg(Convert.ToInt32(Session["Sid"]), Convert.ToInt32(ViewState["IdCateg"]));

                if (cd2 == null)
                {
                    cd.CategDesc = txtRemarkDescCateg.Text;
                    cd.S_id = Convert.ToInt32(Session["Sid"]);
                    cd.Tarif_categoria_id = Convert.ToInt32(ViewState["IdCateg"]);

                    cdd.Salvar(cd);
                    lblMsgCadDesc.Text = "Registro cadastrado com sucesso.";
                }
                else
                {
                    cd.CategDesc = txtRemarkDescCateg.Text;
                    cd.S_id = Convert.ToInt32(Session["Sid"]);
                    cd.Tarif_categoria_id = Convert.ToInt32(ViewState["IdCateg"]);
                    cd.CategDesc_id = Convert.ToInt32(ViewState["IdCategDescr"]);

                    cdd.Atualizar(cd);
                    lblMsgCadDesc.Text = "Registro atualizado com sucesso.";
                }

                
            }
            catch (Exception ex)
            {
                lblMsgCadDesc.Text = ex.Message;
            }
        }

        protected void ExluirRemark(object sender, CommandEventArgs e)
        {
            try
            {
                int myid = Convert.ToInt32(e.CommandArgument);

                CategDescricaoDAL cdd = new CategDescricaoDAL();

                CategDescricao cd = cdd.ObterPorSupCateg(Convert.ToInt32(Session["Sid"]), myid);

                cdd.Excluir(cd);

                lblMsgCadDesc.Text = "Registro excluído com sucesso.";
                
                txtRemarkDescCateg.Text = "";

                PopularGridDesc();
            }
            catch (Exception ex)
            {
                lblMsgCadDesc.Text = ex.Message;
            }
        }

        #endregion

        #region Regras
        

        #endregion

        protected void MostrarMsg(string Msg)
        {
            ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "call me", "showAutoCloseMessage('" + Msg + "')", true);
        }



    }
}